package com.fenxiangtech.mall.service.impl;

import com.fenxiangtech.mall.service.ImageService;
import com.fenxiangtech.mall.dao.ImageDao;
import com.fenxiangtech.mall.entity.Image;
import com.fenxiangtech.mall.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangjiashun
 */
@Service
public class ImageServiceImpl implements ImageService {

    @Autowired
    private ImageDao imageDao;

    @Override
    public Image getImageById(Long imageId) {
        Image image = imageDao.getImageById(imageId);
        this.prefixUrl(image);
        return image;
    }

    @Override
    public List<Image> listImageByProductID(Long productID) {
        List<Image> images = imageDao.listImageByProductID(productID);
        for (Image image : images) {
            this.prefixUrl(image);
        }
        return images;
    }

    private void prefixUrl(Image image) {
        if (image != null) {
            String url = image.getUrl();
            if (image.getFrom() == 1) {
                url = PathUtil.PREFIX_IMG_URL + url;
                image.setUrl(url);
            }
        }
    }
}
